import pygame

import globalvars


class Cursor(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self) # Initialize base class.
		self.image = pygame.Surface([1, 1]).convert_alpha() #self.NoImg
		self.image.fill([0, 0, 0, 0]) #self.NoImg.fill
		self.rect = self.image.get_rect() #self.NoImg.get_rect()
		self.rect.center = pygame.mouse.get_pos()
		globalvars.cursor_rect = self.rect
		self.creating = None
		self.selected = None

	#def Draw(self):
		#if self.isSelecting:
			#return self.SelectImg
		#else:
			#return self.NoImg

	#image = property(Draw, doc="Returns an image that represents the sprite")

	def update(self):
		#self.isSelecting = False
		self.rect = self.image.get_rect() #self.NoImg.get_rect()
		self.rect.center = pygame.mouse.get_pos()
		globalvars.cursor_rect = self.rect
		#if self.Createing == None:
			#for Object in pygame.sprite.spritecollide(self , Globals.ObjectsSelectable , False):
				#self.isSelecting = True
				#self.SelectImg = pygame.Surface([Object.rect.width + 2,Object.rect.height + 2]).convert_alpha()
				#self.rect = self.SelectImg.get_rect()
				#self.SelectImg.fill([0,0,0,0])
				#pygame.draw.rect(self.SelectImg, [0,0,0], self.rect, 1)
				#self.rect.topleft = Object.rect.move(-1,-1).topleft
				#break;

	def create(self, unit):
		if self.creating != None:
			self.creating.kill()
			self.creating = None
		self.creating = unit

	def click_left(self, position):
		if self.creating != None:
			self.creating.create(position)
			self.creating = None
		elif self.selected == None: 
			for unit in pygame.sprite.spritecollide(self , globalvars.objects_selectable , False):
				unit.select()
				self.selected = unit
				break
	
	def click_right(self, position):
		if self.selected != None:
			self.selected.unselect()
			self.selected = None
				
			#pass #TODO: Interface panel and display information
		#else:
			
		#TODO: Interface between objects and Cursor
			#We should be able to create objects without putting them to live, meaning the object to be created handles the drawing under the Cursor
	#	self.rect.center = position
	#	if Globals.NumberOfTuxs > 1:
	#		t = Tux.Tux()
	#		Globals.ObjectsExists.add(t)
	#		Globals.NumberOfTuxs = Globals.NumberOfTuxs -1
		#self.update() #Update current position
		#TODO: If collide object -> Panel.Select(Object)
			#(Panel.image -> Object.Image, Panel.rect -> Object.rect.size + panel.pos)

